package controller

import (
	"blog/dao"
	"github.com/julienschmidt/httprouter"
	"html/template"
	"net/http"
	"strconv"
)

/**
归档
*/
func ArchiveHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)
	t, err := template.New("archive.html").Funcs(dao.FuncMap).ParseFiles("templates/archive.html", "templates/pub_inc.html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	//页号
	pageId, err := strconv.ParseInt(r.URL.Query().Get("page"), 10, 64)
	if err != nil {
		pageId = 1
	}
	if pageId < 1 {
		pageId = 1
	}

	err = t.Execute(w, struct {
		Data dao.PostListData
		Page int64
	}{
		Data: dao.GetPostList(pageId, 30, 0, false),
		Page: pageId,
	})
	if err != nil {
		dao.LogError(err)
		return
	}
}
